{
  "cells": [
    {
      "metadata": {
        "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
        "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\n%matplotlib inline\nimport os\nprint(os.listdir(\"../input\"))\nplt.style.use('ggplot')",
      "execution_count": 1,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
        "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#导入包\nfrom sklearn.base import BaseEstimator,TransformerMixin,RegressorMixin,clone\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import RobustScaler,StandardScaler\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.pipeline import Pipeline,make_pipeline\nfrom scipy.stats import skew\nfrom sklearn.decomposition import PCA,KernelPCA\nfrom sklearn.preprocessing import Imputer",
      "execution_count": 2,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "2d1374d7ad552fb370da9dc77ee66b65f9d628b7",
        "_cell_guid": "42e3dfde-6825-459d-b32f-5387b6a1fd7d",
        "trusted": true
      },
      "cell_type": "code",
      "source": "from sklearn.model_selection import cross_val_score,GridSearchCV,KFold\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.linear_model import Lasso\nfrom sklearn.linear_model import Ridge\nfrom sklearn.linear_model import ElasticNet,SGDRegressor,BayesianRidge\nfrom sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor,ExtraTreesRegressor\nfrom sklearn.svm import SVR,LinearSVR\nfrom sklearn.kernel_ridge import KernelRidge\nfrom xgboost import XGBRegressor",
      "execution_count": 3,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "bbc70fb43b495eeced781b5f7f8066d9834f6956",
        "_cell_guid": "828ffaa5-7778-4366-9c51-bc99576aa428",
        "trusted": true
      },
      "cell_type": "code",
      "source": "train=pd.read_csv('../input/train.csv')\ntest=pd.read_csv('../input/test.csv')\nsubmission=pd.read_csv('../input/sample_submission.csv')",
      "execution_count": 4,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "cf4cb6c74095f072fabb7a91d1adc363b6242191",
        "_cell_guid": "bf5261c4-10c1-4fd5-ba7f-00f21b2c180e",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#探索可视化\nplt.figure(figsize=(15,8))\nsns.boxplot(train.YearBuilt,train.SalePrice)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "572c4ced402e55bb2da91e8cde551eaed0dfc773",
        "_cell_guid": "5d350d1e-4dfa-493a-b2b5-d7fb70f2ba1d",
        "trusted": false
      },
      "cell_type": "code",
      "source": "plt.figure(figsize=(12,6))\nplt.scatter(x=train.GrLivArea,y=train.SalePrice)\nplt.xlabel('GrLivArea',fontsize=13)\nplt.ylabel('SalePrice',fontsize=13)\nplt.ylim(0,800000)\n#显示有两个离群点，删除",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "0557a6d5362629cc07b0febd461888bd253722bc",
        "_cell_guid": "aa542f1f-8170-4834-883b-3f7dee015150",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#删除离群点\ntrain.drop(train[(train['SalePrice']<300000)&(train['GrLivArea']>4000)].index,inplace=True)",
      "execution_count": 5,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "8a2e568d2f71cdbdc3cb0fa3ebfcf14b3a6a6e4b",
        "_cell_guid": "4b37722b-99a3-4c85-b614-c04c1dfebbba",
        "trusted": false
      },
      "cell_type": "code",
      "source": "train.shape",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "6c1b8aa1798ad4eafa492b1cec9154125d1fa97d",
        "_cell_guid": "18b6617c-9cc8-4b02-a003-2dd5c907621a",
        "trusted": true
      },
      "cell_type": "code",
      "source": "full=pd.concat([train,test],ignore_index=True)",
      "execution_count": 6,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "7487fcc05c3dc98a94177814c5502da8ac31be95",
        "_cell_guid": "4ddb348c-8391-46fb-92fb-4d3ff72e3511",
        "trusted": false
      },
      "cell_type": "code",
      "source": "full.shape",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "89ede49c26e9a393f6005e02968394fde66ce1ce",
        "_cell_guid": "b323f7d4-2dbf-45db-9930-9fec2404f147",
        "trusted": false,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "full.drop(['Id'],axis=1,inplace=True)\nfull.shape",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "042824eda914979646188bd0e4b8bb47a349b7dc",
        "_cell_guid": "5bf07243-6bc0-4dfd-af70-bf389e60f751",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#数据清洗\naa=full.isnull().sum()\naa[aa>0].sort_values(ascending=False)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "ca3b32f5fb3b3e13da6e84bfa526c7aac61bdc50",
        "_cell_guid": "32528173-6b3e-4627-89e1-20800ba993e1",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#首先输入LotFrontage,根据LotArea 和 Neighborhood 的中位数。由于LotArea是连续特征，使用qcut将其分为10份\nfull.groupby(['Neighborhood'])[['LotFrontage']].agg(['mean','median','count'])",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "632bf444932d1b78b84a3e662bcd9379c7afc2c2",
        "_cell_guid": "0fe67ec2-d511-4eba-9fb1-7f8cb5b79503",
        "trusted": true
      },
      "cell_type": "code",
      "source": "full['LotAreaCut']=pd.qcut(full.LotArea,10)",
      "execution_count": 7,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "4a0ff942f3d2127e6a0034898e7127d7862716ba",
        "_cell_guid": "a46d9d62-a85c-4ef9-9c3f-3bd04343982b",
        "trusted": false
      },
      "cell_type": "code",
      "source": "full.groupby(['LotAreaCut'])[['LotFrontage']].agg(['mean','median','count'])",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3f182ea23809238e3aa667cd014de0572ca9541d",
        "_cell_guid": "a23ad23b-3a75-445d-9780-e56a0a09ad4f",
        "trusted": true
      },
      "cell_type": "code",
      "source": "full['LotFrontage']=full.groupby(['LotAreaCut','Neighborhood'])['LotFrontage'].transform(lambda x:x.fillna(x.median()))",
      "execution_count": 8,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "c7cc5bf6a3da4fca19cb2c41e1462676bce534eb",
        "_cell_guid": "f4160bf4-dac6-4aa9-bf1f-7d7e6210d06c",
        "trusted": true
      },
      "cell_type": "code",
      "source": "full['LotFrontage']=full.groupby(['LotAreaCut'])['LotFrontage'].transform(lambda x:x.fillna(x.median()))\n#由于LotAreaCut和Neighborhood组合还有空值没有被包括，所以单独采用LotAreaCut再分组",
      "execution_count": 9,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "ce513643a81ae1aa72dea9b3d1629edd207fe267",
        "_cell_guid": "31ae393f-c9fe-45e4-a834-32da0eaf98b2",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#接下来根据数据描述来填充其他缺失值\n#这些都是数值型数据\ncols=['MasVnrArea','BsmtUnfSF','TotalBsmtSF','GarageCars','BsmtFinSF2','BsmtFinSF1','GarageArea']\nfor col in cols:\n    full[col].fillna(0,inplace=True)",
      "execution_count": 10,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "38d118e403f89b024eb06f6c25b02b156d541dfe",
        "_cell_guid": "5fbbbeed-9a75-47e0-8172-e3cffc9d04fe",
        "trusted": true
      },
      "cell_type": "code",
      "source": "cols1=['PoolQC','MiscFeature','Alley','Fence','FireplaceQu','GarageCond','GarageQual','GarageFinish','GarageYrBlt','GarageType','BsmtExposure','BsmtCond','BsmtQual','BsmtFinType2','BsmtFinType1','MasVnrType']\n#缺失的是类别数据\nfor col in cols1:\n    full[col].fillna('None',inplace=True)",
      "execution_count": 11,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3b649a9e8638c7642465ad16ec5e4afb0635f9b5",
        "_cell_guid": "11c7eb82-e760-44aa-b373-330466e705e9",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#根据模式填充\ncols2=['MSZoning','BsmtFullBath','BsmtHalfBath','Utilities','Functional','Electrical','KitchenQual','SaleType','Exterior1st','Exterior2nd']\nfor col in cols2:\n    full[col].fillna(full[col].mode()[0],inplace=True)",
      "execution_count": 12,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "988030b5288463ae52c89218540f307e720e3d82",
        "_cell_guid": "bc0c17f0-c791-47a8-9782-3818043980a1",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "full.isnull().sum()[full.isnull().sum()>0]",
      "execution_count": 13,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "39fcddcf9d6719fd5a75daefc34d3e86fca7d0d0",
        "_cell_guid": "cb4c96c1-d401-46c0-b63a-139f1723ffd3",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#full['LowQualFinSF'].unique()",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "4a57bec2b6cbd73a54051787c2b327c0aa29ea23",
        "_cell_guid": "ebc72d79-e636-44b1-8a52-4780c34f4957",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#特征工程\n#1.转换一些数值特征为类别特征。然后对这些特征使用LabelEncoder 和 get_dummies   ,\"LowQualFinSF\"\nNumStr=['MSSubClass','BsmtFullBath','BsmtHalfBath','HalfBath','BedroomAbvGr','KitchenAbvGr','MoSold','YrSold','YearBuilt','YearRemodAdd','GarageYrBlt']\nfor col in NumStr:\n    full[col]=full[col].astype(str)",
      "execution_count": 14,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "36a1d6f1fc78fcb224d872fd14c7d6a651157aac",
        "_cell_guid": "0be1f6d1-e944-4526-bc5e-148c4f416b3d",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "full.groupby(['HouseStyle'])[['SalePrice']].agg(['mean','median','count'])",
      "execution_count": 15,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "b79210edf61b658acecf310a230de553b08809b0",
        "_cell_guid": "bf697439-4bbb-4f0a-8d42-c9ca27cbd58d",
        "trusted": true
      },
      "cell_type": "code",
      "source": "def map_values():\n    full[\"oMSSubClass\"] = full.MSSubClass.map({'180':1, \n                                        '30':2, '45':2, \n                                        '190':3, '50':3, '90':3, \n                                        '85':4, '40':4, '160':4, \n                                        '70':5, '20':5, '75':5, '80':5, '150':5,\n                                        '120': 6, '60':6})\n    \n    full[\"oMSZoning\"] = full.MSZoning.map({'C (all)':1, 'RH':2, 'RM':2, 'RL':3, 'FV':4})\n    \n    full[\"oNeighborhood\"] = full.Neighborhood.map({'MeadowV':1,\n                                               'IDOTRR':2, 'BrDale':2,\n                                               'OldTown':3, 'Edwards':3, 'BrkSide':3,\n                                               'Sawyer':4, 'Blueste':4, 'SWISU':4, 'NAmes':4,\n                                               'NPkVill':5, 'Mitchel':5,\n                                               'SawyerW':6, 'Gilbert':6, 'NWAmes':6,\n                                               'Blmngtn':7, 'CollgCr':7, 'ClearCr':7, 'Crawfor':7,\n                                               'Veenker':8, 'Somerst':8, 'Timber':8,\n                                               'StoneBr':9,\n                                               'NoRidge':10, 'NridgHt':10})\n    \n    full[\"oCondition1\"] = full.Condition1.map({'Artery':1,\n                                           'Feedr':2, 'RRAe':2,\n                                           'Norm':3, 'RRAn':3,\n                                           'PosN':4, 'RRNe':4,\n                                           'PosA':5 ,'RRNn':5})\n    \n    full[\"oBldgType\"] = full.BldgType.map({'2fmCon':1, 'Duplex':1, 'Twnhs':1, '1Fam':2, 'TwnhsE':2})\n    \n    full[\"oHouseStyle\"] = full.HouseStyle.map({'1.5Unf':1, \n                                           '1.5Fin':2, '2.5Unf':2, 'SFoyer':2, \n                                           '1Story':3, 'SLvl':3,\n                                           '2Story':4, '2.5Fin':4})\n    \n    full[\"oExterior1st\"] = full.Exterior1st.map({'BrkComm':1,\n                                             'AsphShn':2, 'CBlock':2, 'AsbShng':2,\n                                             'WdShing':3, 'Wd Sdng':3, 'MetalSd':3, 'Stucco':3, 'HdBoard':3,\n                                             'BrkFace':4, 'Plywood':4,\n                                             'VinylSd':5,\n                                             'CemntBd':6,\n                                             'Stone':7, 'ImStucc':7})\n    \n    full[\"oMasVnrType\"] = full.MasVnrType.map({'BrkCmn':1, 'None':1, 'BrkFace':2, 'Stone':3})\n    \n    full[\"oExterQual\"] = full.ExterQual.map({'Fa':1, 'TA':2, 'Gd':3, 'Ex':4})\n    \n    full[\"oFoundation\"] = full.Foundation.map({'Slab':1, \n                                           'BrkTil':2, 'CBlock':2, 'Stone':2,\n                                           'Wood':3, 'PConc':4})\n    \n    full[\"oBsmtQual\"] = full.BsmtQual.map({'Fa':2, 'None':1, 'TA':3, 'Gd':4, 'Ex':5})\n    \n    full[\"oBsmtExposure\"] = full.BsmtExposure.map({'None':1, 'No':2, 'Av':3, 'Mn':3, 'Gd':4})\n    \n    full[\"oHeating\"] = full.Heating.map({'Floor':1, 'Grav':1, 'Wall':2, 'OthW':3, 'GasW':4, 'GasA':5})\n    \n    full[\"oHeatingQC\"] = full.HeatingQC.map({'Po':1, 'Fa':2, 'TA':3, 'Gd':4, 'Ex':5})\n    \n    full[\"oKitchenQual\"] = full.KitchenQual.map({'Fa':1, 'TA':2, 'Gd':3, 'Ex':4})\n    \n    full[\"oFunctional\"] = full.Functional.map({'Maj2':1, 'Maj1':2, 'Min1':2, 'Min2':2, 'Mod':2, 'Sev':2, 'Typ':3})\n    \n    full[\"oFireplaceQu\"] = full.FireplaceQu.map({'None':1, 'Po':1, 'Fa':2, 'TA':3, 'Gd':4, 'Ex':5})\n    \n    full[\"oGarageType\"] = full.GarageType.map({'CarPort':1, 'None':1,\n                                           'Detchd':2,\n                                           '2Types':3, 'Basment':3,\n                                           'Attchd':4, 'BuiltIn':5})\n    \n    full[\"oGarageFinish\"] = full.GarageFinish.map({'None':1, 'Unf':2, 'RFn':3, 'Fin':4})\n    \n    full[\"oPavedDrive\"] = full.PavedDrive.map({'N':1, 'P':2, 'Y':3})\n    \n    full[\"oSaleType\"] = full.SaleType.map({'COD':1, 'ConLD':1, 'ConLI':1, 'ConLw':1, 'Oth':1, 'WD':1,\n                                       'CWD':2, 'Con':3, 'New':3})\n    \n    full[\"oSaleCondition\"] = full.SaleCondition.map({'AdjLand':1, 'Abnorml':2, 'Alloca':2, 'Family':2, 'Normal':3, 'Partial':4})            \n                \n                        \n                        \n    \n    return \"Done!\"",
      "execution_count": 16,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "98976ca6a14c96223487585c21ead9e1de885c56",
        "_cell_guid": "904b8db0-9861-428d-b30a-9ca56a2194c8",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "map_values()",
      "execution_count": 17,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "c993ff9c5b79943310259dd001c3b39fa1ce3f08",
        "_cell_guid": "a6736e1e-d7d1-4a7f-92fb-3cb9500117a8",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#丢弃两个不需要特征\nfull.drop('LotAreaCut',axis=1,inplace=True)\nfull.drop('SalePrice',axis=1,inplace=True)",
      "execution_count": 18,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "ed36a01e4e2b28e588ffdc8e2ffa302f3c063acd",
        "_cell_guid": "caef2e6a-c734-444e-9ab9-cc548e6da51a",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#PipeLine 好处：可以很方便的测试不同的特征组合\n#标签编码三个‘Year’特征\nclass labelenc(BaseEstimator,TransformerMixin):\n    def __inti__(self):\n        pass\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        lab=LabelEncoder()\n        X['YearBuilt']=lab.fit_transform(X['YearBuilt'])\n        X['YearRemodAdd']=lab.fit_transform(X['YearRemodAdd'])\n        X['GarageYrBlt']=lab.fit_transform(X['GarageYrBlt'])\n        return X",
      "execution_count": 19,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "fe493bcf7acadfe1abdcb1f50f488b6f8d90a94f",
        "_cell_guid": "c1a11514-bdd8-490e-9786-4c23e1f2e0cd",
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 对有偏度的特征使用log变换，然后get_dummies\nclass skew_dummies(BaseEstimator,TransformerMixin):\n    def __init__(self,skew=0.5):\n        self.skew=skew\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        X_numeric=X.select_dtypes(exclude=['object'])\n        skewness=X_numeric.apply(lambda x:skew(x))\n        skewness_feature=skewness[abs(skewness)>=self.skew].index\n        X[skewness_feature]=np.log1p(X[skewness_feature])\n        X=pd.get_dummies(X)\n        return X",
      "execution_count": 20,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "1374d1a02ad115a92730147ee38efa2069039f69",
        "_cell_guid": "ddc35c61-4b15-420d-9113-49c45c0a10f2",
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 建立pipeline,就是将几个函数集合到一起了\npipe=Pipeline([\n    ('labenc',labelenc()),\n    ('skew_dummies',skew_dummies(skew=1)),\n])",
      "execution_count": 21,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "dd809dbb7069b56d9b6a6f1fdd3cdd63501bf8c6",
        "_cell_guid": "79d723aa-61b2-4bf0-8356-e31e53f1ba88",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#保存最初变量以后使用\nfull2=full.copy()",
      "execution_count": 22,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "c0db867d4d668f35c8a8c3b632d58d0dda148758",
        "_cell_guid": "41dea93e-9b8c-4637-a50c-5db2c2bda54b",
        "trusted": true
      },
      "cell_type": "code",
      "source": "data_pipe=pipe.fit_transform(full2)",
      "execution_count": 23,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "d792e92dfd3b2d56de207c3f3c910470de7cac84",
        "_cell_guid": "aa9a8113-994d-4260-8ece-b9680f69c3df",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "data_pipe.shape",
      "execution_count": 24,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "55f22f011ea99e3f3817848c3124737ea42cec41",
        "_cell_guid": "894b34cf-a8a6-47d4-a4e8-faa132e669ac",
        "trusted": false
      },
      "cell_type": "code",
      "source": "data_pipe.head()",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "e4146779326c182412e465752fe4a82fbae5dde0",
        "_cell_guid": "d0bb1cd0-0bf2-4aa0-aefb-e7155550feca",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#使用robustscaler以防还有其他离群点 （强大的缩放器）\nscaler=RobustScaler()\nn_train=train.shape[0]\nX=data_pipe[:n_train]\ntest_X=data_pipe[n_train:]\ny=train.SalePrice\n\nX_scaled=scaler.fit(X).transform(X)\ny_log=np.log(train.SalePrice)\ntest_X_scaled=scaler.transform(test_X)",
      "execution_count": 25,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "c40dae80fae6d30babf88a7d2b3c999f2c274afe",
        "_cell_guid": "6c0df888-c56f-40e8-81f2-6306fc663e08",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#特征选择 Lasspo Ridge RandomForest GradientBoosting Tree\nlasso=Lasso(alpha=0.001)\nlasso.fit(X_scaled,y_log)",
      "execution_count": 26,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "4623afeaa2c8b9bef5cb2608672ca5c0873363f0",
        "_cell_guid": "22f79002-4bcc-4dfa-972d-821a4e359d77",
        "trusted": false
      },
      "cell_type": "code",
      "source": "FI_lasso=pd.DataFrame({'Feature Importance':lasso.coef_},index=data_pipe.columns)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "1d8394f2568b5dc0f6e82496f962298a3a7b8c9d",
        "_cell_guid": "6eda8941-4219-4ce1-925d-5913f01c2eaa",
        "trusted": false,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "FI_lasso.sort_values('Feature Importance',ascending=False)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "1aea4f8fbbf7ec37be05d8343533c33e5788135a",
        "_cell_guid": "c9508ad8-7bf9-42a9-a4f5-d194492808d0",
        "trusted": false
      },
      "cell_type": "code",
      "source": "FI_lasso[FI_lasso['Feature Importance']!=0].sort_values('Feature Importance').plot(kind='barh',figsize=(15,25))\nplt.xticks(rotation=90)\nplt.show()",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "c538f633da4164a502c1ac2fe5a132e2510a4e3a",
        "_cell_guid": "00af9944-f4aa-4de5-bce8-ad40f860cc02",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#根据特征重要性信息，在pipeline中做一些新添一些特征\nclass add_feature(BaseEstimator, TransformerMixin):\n    def __init__(self,additional=1):\n        self.additional = additional\n    \n    def fit(self,X,y=None):\n        return self\n    \n    def transform(self,X):\n        if self.additional==1:\n            X[\"TotalHouse\"] = X[\"TotalBsmtSF\"] + X[\"1stFlrSF\"] + X[\"2ndFlrSF\"]   \n            X[\"TotalArea\"] = X[\"TotalBsmtSF\"] + X[\"1stFlrSF\"] + X[\"2ndFlrSF\"] + X[\"GarageArea\"]\n            \n        else:\n            X[\"TotalHouse\"] = X[\"TotalBsmtSF\"] + X[\"1stFlrSF\"] + X[\"2ndFlrSF\"]   \n            X[\"TotalArea\"] = X[\"TotalBsmtSF\"] + X[\"1stFlrSF\"] + X[\"2ndFlrSF\"] + X[\"GarageArea\"]\n            \n            X[\"+_TotalHouse_OverallQual\"] = X[\"TotalHouse\"] * X[\"OverallQual\"]\n            X[\"+_GrLivArea_OverallQual\"] = X[\"GrLivArea\"] * X[\"OverallQual\"]\n            X[\"+_oMSZoning_TotalHouse\"] = X[\"oMSZoning\"] * X[\"TotalHouse\"]\n            X[\"+_oMSZoning_OverallQual\"] = X[\"oMSZoning\"] + X[\"OverallQual\"]\n            X[\"+_oMSZoning_YearBuilt\"] = X[\"oMSZoning\"] + X[\"YearBuilt\"]\n            X[\"+_oNeighborhood_TotalHouse\"] = X[\"oNeighborhood\"] * X[\"TotalHouse\"]\n            X[\"+_oNeighborhood_OverallQual\"] = X[\"oNeighborhood\"] + X[\"OverallQual\"]\n            X[\"+_oNeighborhood_YearBuilt\"] = X[\"oNeighborhood\"] + X[\"YearBuilt\"]\n            X[\"+_BsmtFinSF1_OverallQual\"] = X[\"BsmtFinSF1\"] * X[\"OverallQual\"]\n            \n            X[\"-_oFunctional_TotalHouse\"] = X[\"oFunctional\"] * X[\"TotalHouse\"]\n            X[\"-_oFunctional_OverallQual\"] = X[\"oFunctional\"] + X[\"OverallQual\"]\n            X[\"-_LotArea_OverallQual\"] = X[\"LotArea\"] * X[\"OverallQual\"]\n            X[\"-_TotalHouse_LotArea\"] = X[\"TotalHouse\"] + X[\"LotArea\"]\n            X[\"-_oCondition1_TotalHouse\"] = X[\"oCondition1\"] * X[\"TotalHouse\"]\n            X[\"-_oCondition1_OverallQual\"] = X[\"oCondition1\"] + X[\"OverallQual\"]\n            \n           \n            X[\"Bsmt\"] = X[\"BsmtFinSF1\"] + X[\"BsmtFinSF2\"] + X[\"BsmtUnfSF\"]\n            X[\"Rooms\"] = X[\"FullBath\"]+X[\"TotRmsAbvGrd\"]\n            X[\"PorchArea\"] = X[\"OpenPorchSF\"]+X[\"EnclosedPorch\"]+X[\"3SsnPorch\"]+X[\"ScreenPorch\"]\n            X[\"TotalPlace\"] = X[\"TotalBsmtSF\"] + X[\"1stFlrSF\"] + X[\"2ndFlrSF\"] + X[\"GarageArea\"] + X[\"OpenPorchSF\"]+X[\"EnclosedPorch\"]+X[\"3SsnPorch\"]+X[\"ScreenPorch\"]\n\n    \n            return X",
      "execution_count": 27,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "aca5a1e8b33d05fd86f91cca3076e6325483db56",
        "_cell_guid": "592ef515-dea1-4c2e-894a-2ec852e88571",
        "trusted": true
      },
      "cell_type": "code",
      "source": "pipe=Pipeline([\n    ('labenc',labelenc()),\n    ('add_feature',add_feature(additional=2)),\n    ('skew_dummies',skew_dummies(skew=1)),\n])",
      "execution_count": 28,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "8e9836a55c78d0c06c5dbcd5b10d1a07095ce72c",
        "_cell_guid": "26ccf61d-0af0-431a-8a71-252702d1ace7",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#PCA降维\nfull_pipe=pipe.fit_transform(full)\nfull_pipe.shape",
      "execution_count": 29,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "169d4a454a1b4b153cc34beda77317f588f2597c",
        "_cell_guid": "7ba07861-3abf-4f6c-92be-85bd50b1c639",
        "trusted": true
      },
      "cell_type": "code",
      "source": "n_train=train.shape[0]\nX=full_pipe[:n_train]\ntest_X=full_pipe[n_train:]\ny=train.SalePrice\n\nX_scaled=scaler.fit(X).transform(X)\ny_log=np.log(train.SalePrice)\ntest_X_scaled=scaler.transform(test_X)",
      "execution_count": 30,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "be3d43bf2982cf4743c7e164f6c218d6b4a4feee",
        "_cell_guid": "8e7be5fc-cb02-4c2b-ae17-ebb55f5b7561",
        "trusted": true
      },
      "cell_type": "code",
      "source": "pca=PCA(n_components=350)",
      "execution_count": 31,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "f251d9338a97bc9b589e427d8a206755bbe08481",
        "_cell_guid": "983f01b0-227a-4185-b91d-17c75dc2363c",
        "trusted": true
      },
      "cell_type": "code",
      "source": "X_scaled=pca.fit_transform(X_scaled)\ntest_X_scaled=pca.transform(test_X_scaled)",
      "execution_count": 32,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "426a0a897a80d248cf033dac99e7254698461122",
        "_cell_guid": "0d9e0f3b-b3b4-489c-8ca6-f4cce853f188",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "X_scaled.shape, test_X_scaled.shape",
      "execution_count": 33,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3b1982c519ab550c5297e76d9c189b54f6e232e3",
        "_cell_guid": "e8bf2d76-38cb-4c66-b6d8-cf147511e979",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#模型和验证\ndef rmse_cv(model,X,y):\n    rmse=np.sqrt(-cross_val_score(model,X,y,scoring='neg_mean_squared_error',cv=5))\n    return rmse",
      "execution_count": 34,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "95b7d2cfb9e08e5edcccfdeeac32ea5fa751f893",
        "_cell_guid": "5c34b0d4-0a2b-4a20-8a29-3bd69baf88e6",
        "trusted": false
      },
      "cell_type": "code",
      "source": "models=[LinearRegression(),Ridge(),Lasso(alpha=0.1,max_iter=10000),RandomForestRegressor(),GradientBoostingRegressor(),SVR(),LinearSVR(),ElasticNet(alpha=0.001,max_iter=10000),\n       SGDRegressor(max_iter=1000,tol=1e-3),BayesianRidge(),KernelRidge(alpha=0.6,kernel='polynomial',degree=2,coef0=2.5),ExtraTreesRegressor(),XGBRegressor()]",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "e7597f9cf931149aafeb57d321ea0a18abca01ea",
        "_cell_guid": "58414e0e-781a-41a3-ae7f-b9e88266f495",
        "trusted": false
      },
      "cell_type": "code",
      "source": "names=['LR','Ridge','Lasso','RF','GBR','SVR','LinSVR','Ela','SGD','Bay','Ker','Extra','Xgb']\nfor name,model in zip(names,models):\n    score=rmse_cv(model,X_scaled,y_log)\n    print(\"{}:{:.6f},{:.4f}\".format(name,score.mean(),score.std()))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3fe177562873bff70a41bff3bb05a8db8bdf986f",
        "_cell_guid": "be128d85-840c-4fe4-8ba8-0c3b49966097",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#超参优化\nclass grid():\n    def __init__(self,model):\n        self.model=model\n    def grid_get(self,X,y,param_grid):\n        grid_search=GridSearchCV(self.model,param_grid,cv=5,scoring='neg_mean_squared_error',n_jobs=-1)\n        grid_search.fit(X,y)\n        print(grid_search.best_params_,np.sqrt(-grid_search.best_score_))\n        grid_search.cv_results_['mean_test_score']=np.sqrt(-grid_search.cv_results_['mean_test_score'])\n        print(pd.DataFrame(grid_search.cv_results_)[['params','mean_test_score','std_test_score']])",
      "execution_count": 35,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "850d78320687a5fac17de36e19578a432e1f5a58",
        "_cell_guid": "22c038e5-a690-4ad7-9a8e-64f9bc834b83",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#grid(Lasso()).grid_get(X_scaled,y_log,{'alpha':[0.0001,0.0002,0.0003,0.0004,0.0005,0.0006,0.0007,0.0008,0.0009]})",
      "execution_count": 40,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "ef348e346d6ba62a72e05651669e00318395776b",
        "_cell_guid": "6cd98b73-09fe-4daf-9acf-1acdf91d73d3",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#grid(Ridge()).grid_get(X_scaled,y_log,{'alpha':[60]})",
      "execution_count": 39,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "b88fa891978294b550a2887e7dad06580310383b",
        "_cell_guid": "82d4acaf-a8e5-4bf8-b57b-602a195d6b5a",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#grid(SVR()).grid_get(X_scaled,y_log,{'C':[11,12,13,14],'epsilon':[0.009,0.01,0.02],'gamma':[0.0002,0.0003,0.0004,0.0005],'kernel':['rbf']})",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "61b928ab91f8dcd8277a409fe71e5b80e40e7f88",
        "_cell_guid": "0104940b-02d3-4a70-9083-bd90ea168a3c",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#param_grid={'alpha':[0.1,0.2,0.5],'coef0':[0.8,1.0,14,15,20,25],'degree':[2,3,4],'kernel':['polynomial']}\n#grid(KernelRidge()).grid_get(X_scaled,y_log,param_grid)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "abe95e0cfda88bd19c081b530296992decfdaf84",
        "_cell_guid": "8f76e851-16c3-493d-80e7-27373337db02",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#grid(ElasticNet()).grid_get(X_scaled,y_log,{'alpha':[0.04,0.05,0.07,0.09],'l1_ratio':[0.04,0.03,0.02,0.01,0]})",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "4b52b4d0a05103fad40d344409c0593baaaddc28",
        "_cell_guid": "44673ebb-cb13-4657-b9bf-10390bc17671",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#集成方法 根据他们的权重来取均值\nclass AverageWeight(BaseEstimator,RegressorMixin):\n    def __init__(self,mod,weight):\n        self.mod=mod\n        self.weight=weight\n    def fit(self,X,y):\n        self.models_=[clone(x) for x in self.mod]\n        for model in self.models_:\n            model.fit(X,y)\n        return self\n    def predict(self,X):\n        w=list()\n        pred=np.array([model.predict(X) for model in self.models_])\n        for data in range(pred.shape[1]):\n            single=[pred[model,data]*weight for model ,weight in zip(range(pred.shape[0]),self.weight)]\n            w.append(np.sum(single))\n        return w",
      "execution_count": 41,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "bc0089f4d181078eb630e6b1aedb04596d187881",
        "_cell_guid": "9f912a70-61f6-4b67-acae-1a1fae96b103",
        "trusted": true
      },
      "cell_type": "code",
      "source": "lasso=Lasso(alpha=0.0005)#0.11222985252930259\nridge=Ridge(alpha=60)# 0.11029403244945726\nsvr=SVR(C=12,epsilon=0.01,gamma=0.0004,kernel='rbf')#0.10877461055690453{'C': 12, 'epsilon': 0.01, 'gamma': 0.0004, 'kernel': 'rbf'} 0.10816083998052767\nker=KernelRidge(alpha=0.5,coef0=1.0,degree=3,kernel='polynomial')#0.10833058500999447\nela=ElasticNet(alpha=0.05,l1_ratio=0)#0.11029574008657018\nbay=BayesianRidge()#Bay:0.110668,0.0060",
      "execution_count": 42,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "357f2b2e13a1ece72f954ac1c528926b427ae933",
        "_cell_guid": "1502a116-ba14-4013-a838-cbf3cea535d3",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#根据 gridsearch score 设置权重\nw1=0.02\nw2=0.2\nw3=0.25\nw4=0.3\nw5=0.03\nw6=0.2",
      "execution_count": 43,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "358c00d37342965ffea6c0d6515886447337d776",
        "_cell_guid": "d526041b-f4e7-4b08-a8a0-d7c17a50d622",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#weight_avg=AverageWeight(mod=[lasso,ridge,svr,ker,ela,bay],weight=[w1,w2,w3,w4,w5,w6])",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "997165f92dc9102782d98173ce33746d0d6d3ff6",
        "_cell_guid": "3b937e14-1f1f-4b5c-992e-8a1ae6a635b4",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#score=rmse_cv(weight_avg,X_scaled,y_log)\n#print(score.mean())",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "81ae7a93d1f147ee178edad5b8e418dbfd3df44f",
        "_cell_guid": "9d7e98e2-7908-4d1a-b180-4fc09310409f",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#如果只平均俩个最好的model 结果更好一点\n#weight_avg=AverageWeight(mod=[svr,ker],weight=[0.5,0.5])\n#score=rmse_cv(weight_avg,X_scaled,y_log)\n#print(score.mean())",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "a574ceb2d0b073698c4c5ec70926ba4806d9b56a",
        "_cell_guid": "6cd9e290-f586-4be0-8acc-5733cec9e809",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#堆叠 除了正常的堆叠，还要增加 get_oof 方法，因为之后我将组合堆叠生成的特征和原先的特征\nclass stacking(BaseEstimator,RegressorMixin,TransformerMixin):\n    def __init__(self,mod,meta_model):\n        self.mod=mod\n        self.meta_model=meta_model\n        self.kf=KFold(n_splits=5,random_state=42,shuffle=True)\n    def fit(self,X,y):\n        self.saved_model=[list() for i in self.mod]\n        oof_train=np.zeros((X.shape[0],len(self.mod)))\n        for i,model in enumerate(self.mod):\n            for train_index,val_index in self.kf.split(X,y):\n                renew_model=clone(model)\n                renew_model.fit(X[train_index],y[train_index])\n                self.saved_model[i].append(renew_model)\n                oof_train[val_index,i]=renew_model.predict(X[val_index])\n        self.meta_model.fit(oof_train,y)\n        return self\n    def predict(self,X):\n        whole_test=np.column_stack([np.column_stack(model.predict(X) for model in single_model).mean(axis=1) for single_model in self.saved_model])\n        return self.meta_model.predict(whole_test)\n   \n    def get_oof(self,X,y,test_X):\n        oof=np.zeros((X.shape[0],len(self.mod)))\n        test_single=np.zeros((test_X.shape[0],5))\n        test_mean=np.zeros((test_X.shape[0],len(self.mod)))\n        for i,model in enumerate(self.mod):\n            for j,(train_index,val_index) in enumerate(self.kf.split(X,y)):\n                clone_model=clone(model)\n                clone_model.fit(X[train_index],y[train_index])\n                oof[val_index,i]=clone_model.predict(X[val_index])\n                test_single[:,j]=clone_model.predict(test_X)\n            test_mean[:,i]=test_single.mean(axis=1)\n        return oof,test_mean",
      "execution_count": 44,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "5b971b5a27324fa10abf83a19ec5f12969d227ad",
        "_cell_guid": "297ec4b8-f4e7-49ef-b31a-512b82103d60",
        "trusted": true
      },
      "cell_type": "code",
      "source": "a=Imputer().fit_transform(X_scaled)\nb=Imputer().fit_transform(y_log.values.reshape(-1,1)).ravel()",
      "execution_count": 45,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "cdde937f4e4bc3db1afec58f4280b6bc648840b4",
        "_cell_guid": "86a4d2c5-704f-4e9d-abc6-07535c6c55df",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "a.shape,b.shape,test_X_scaled.shape",
      "execution_count": 46,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "9f040700d1216eaa8c650da6366640c9f2571977",
        "_cell_guid": "32a652a5-cce7-4e7b-b9a7-9139e7ac2978",
        "trusted": true
      },
      "cell_type": "code",
      "source": "stack_model=stacking(mod=[lasso,ridge,svr,ker,ela,bay],meta_model=ker)",
      "execution_count": 47,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "16d000a7c9fc1fb487ef45066e998220e09d1e91",
        "_cell_guid": "bfecf3d8-0f93-452c-8edc-ce234a5a0a8a",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#score=rmse_cv(stack_model,a,b)\n#print(score.mean())",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "c0ce6f2a98715a8ae30a31a2e8576d483d05ffaa",
        "_cell_guid": "56b2cb25-64f9-4d58-b673-d6f9bdcbdff5",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#现在我们提取堆叠生成的特征，然后和先前特征组合\nX_train_stack,X_test_stack=stack_model.get_oof(a,b,test_X_scaled)",
      "execution_count": 48,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "454ba31440f9afe24616ed8c6247471cb1e5532b",
        "_cell_guid": "847b660a-5e88-4059-93cb-be494c932349",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "X_train_stack.shape,X_test_stack.shape",
      "execution_count": 49,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "b832a86aa20aa561edec2ff99de8ebd5647c8d14",
        "_cell_guid": "542ac893-6cd0-481b-95b8-69b49842ed96",
        "trusted": true
      },
      "cell_type": "code",
      "source": "#X_train_stack[:1],a[:1]\nX_train_add=np.hstack((a,X_train_stack))\nX_test_add=np.hstack((test_X_scaled,X_test_stack))",
      "execution_count": 50,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "4d0bc4ffe6d7c6fd835a2134edd14e17476b3a7a",
        "_cell_guid": "09e7b50f-93d4-4dbb-ad2c-012d494a674f",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "X_train_add.shape,X_test_add.shape",
      "execution_count": 51,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3f45e4ee1a7f2b228c03ff2d3482e82f8536c36a",
        "_cell_guid": "4db2b768-f446-4fcd-a18b-22bec3813039",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#组合特征后的超参优化\n#grid(Lasso()).grid_get(X_train_add,b,{'alpha': [0.00001,0.0001,0.0002],'max_iter':[10000]})",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "832ec7a49a51c1a5db291bd4106cab91ef740279",
        "_cell_guid": "b217ff53-c84a-41d6-beed-6857ae8cb3c3",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#grid(Ridge()).grid_get(X_train_add,b,{'alpha':[0.05,0.07,0.08,0.09,0.1]})",
      "execution_count": 53,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "114557d6a6b1540da7e580ad0b5cff4fb62b7705",
        "_cell_guid": "508417b1-c7aa-4312-a5c3-3be5df12dd7c",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#grid(SVR()).grid_get(X_train_add,b,{'C':[8,9,11],'kernel':[\"rbf\"],\"gamma\":[0.001,0.002,0.004],\"epsilon\":[0.009,0.01,0.02]})",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "45f53bb73bca9b822ecf056441d011dc51ed728a",
        "_cell_guid": "c68ae496-d112-4ba1-9cf6-05c6a150fe6f",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#param_grid={'alpha':[0.007,0.009], 'kernel':[\"polynomial\"], 'degree':[2,3],'coef0':[1.5,2.0,3.0,4.0]}\n#grid(KernelRidge()).grid_get(X_train_add,b,param_grid)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "26990ff8c56d455e9e0ab60f2150679f92276754",
        "_cell_guid": "abf5c7aa-2cc4-4a0d-926c-f5f1268cf2dd",
        "trusted": false
      },
      "cell_type": "code",
      "source": "#grid(ElasticNet()).grid_get(X_train_add,b,{'alpha':[0.00001,0.0001,0.0002,0.0003],'l1_ratio':[0.5,0.6,0.7],'max_iter':[10000]})",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "26d013dfda6552a240c5e9c5f2eb3d554bd865f5",
        "_cell_guid": "0606a7bd-2039-454b-b43c-c97b5308e9ac",
        "trusted": true
      },
      "cell_type": "code",
      "source": "lasso = Lasso(alpha=0.0001,max_iter=10000)#{'alpha': 0.0005, 'max_iter': 10000} 0.11129660796478734 {'alpha': 0.0001, 'max_iter': 10000} 0.09226663454775956\nridge = Ridge(alpha=60)#{'alpha': 60} 0.11020174961498394  {'alpha': 0.09} 0.10714499635266204\nsvr = SVR(gamma= 0.001,kernel='rbf',C=11,epsilon=0.01)#{'C': 13, 'epsilon': 0.009, 'gamma': 0.0004, 'kernel': 'rbf'} 0.10823195916311412 {'C': 11, 'epsilon': 0.01, 'gamma': 0.001, 'kernel': 'rbf'} 0.10590847066839579\nker = KernelRidge(alpha=0.007 ,kernel='polynomial',degree=2 , coef0=4.0)#{'alpha': 0.2, 'coef0': 0.8, 'degree': 3, 'kernel': 'polynomial'} 0.10826973614765958 {'alpha': 0.007, 'coef0': 4.0, 'degree': 2, 'kernel': 'polynomial'} 0.10423535790468955\nela = ElasticNet(alpha=0.0001,l1_ratio=0.6,max_iter=10000)#{'alpha': 0.005, 'l1_ratio': 0.08, 'max_iter': 10000} 0.11117135158453571  {'alpha': 0.0001, 'l1_ratio': 0.6, 'max_iter': 10000} 0.09202893573835882\n#bay = BayesianRidge()#Bay: 0.110577, 0.0060",
      "execution_count": 54,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "faf9347107108c3b0aa414c27f5dca2cfaf03a35",
        "_cell_guid": "504ceafd-6cf6-44f3-b531-bb3a14aa312e",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "X_train_add.shape, X_test_add.shape",
      "execution_count": 55,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "3cdf01a29707f86d1ffad24ec2b18961494d16a3",
        "_cell_guid": "01a92a79-545a-4797-95e5-f4ddef7f8d32",
        "trusted": false
      },
      "cell_type": "code",
      "source": "score=rmse_cv(stack_model,X_train_add,b)\nprint(score.mean())",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "12ef182e90b60ad74d86c6551d1a19bf7412f492",
        "_cell_guid": "54aa5a92-fbaf-4b0b-8663-10cd2b8b308c",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "#提交\nstack_model=stacking(mod=[lasso,ridge,svr,ker,ela],meta_model=ela)\nstack_model.fit(X_train_add,b)",
      "execution_count": 56,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "bf0170adf3ecb9a40f6ca44c0fbd8333e4a4356d",
        "_cell_guid": "1a7b4496-9594-4c7a-a3d0-cd56fe9f55b4",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "a.shape,b.shape,X_test_add.shape",
      "execution_count": 57,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "54b797f212c8c8185ed954c731f6932b208ca7b6",
        "_cell_guid": "fd7dcbc6-ba0d-4c39-852e-91605af2eb60",
        "trusted": true
      },
      "cell_type": "code",
      "source": "submission['SalePrice']=np.exp(stack_model.predict(X_test_add))",
      "execution_count": 58,
      "outputs": []
    },
    {
      "metadata": {
        "_uuid": "e5a976068205cf50c038d861c2e10379ed28629a",
        "_cell_guid": "f7fcd63d-55e7-4d7a-8cbb-ee1ecb8efb8b",
        "trusted": true,
        "collapsed": true
      },
      "cell_type": "code",
      "source": "submission.head()",
      "execution_count": 59,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "1f2307733cb54a552d6da4ebcc7e0a6600c20152",
        "_cell_guid": "5dd19dc5-1088-44fd-9276-33e00a203eed",
        "trusted": false
      },
      "cell_type": "code",
      "source": "'''param_grid={'alpha':[0.0001,0.0005,0.0006,0.0007,0.0008,0.0009,0.001,0.003,0.004,0.005,0.01]}\ngrid_search=GridSearchCV(Lasso(),param_grid,cv=5,scoring='neg_mean_squared_error',n_jobs=-1)\ngrid_search.fit(X_scaled,y_log)\nsubmission['SalePrice']=np.exp(grid_search.predict(test_scaled))'''",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "97ad121d05580513d47a75bc64624a2cbc3795f6",
        "_cell_guid": "08e4dada-c08a-47a7-b407-0a236c718f44",
        "trusted": false
      },
      "cell_type": "code",
      "source": "'''print(grid_search.best_params_,np.sqrt(-grid_search.best_score_))\ngrid_search.cv_results_['mean_test_score']=np.sqrt(-grid_search.cv_results_['mean_test_score'])\nprint(pd.DataFrame(grid_search.cv_results_)[['params','mean_test_score','std_test_score']])'''",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "collapsed": true,
        "_uuid": "ddac81e207ca8fe693f8934b17c1d2560497f4b3",
        "_cell_guid": "b2fed32a-6ef2-44fe-9a8f-275ad8d9dceb",
        "trusted": true
      },
      "cell_type": "code",
      "source": "submission.to_csv('submission.csv',index=False)",
      "execution_count": 60,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.5",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 1
}